package com.quectel.core.module.devicepatrol.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.devicepatrol.dao.DevicePatrolRouteDeviceDao;
import com.quectel.core.module.devicepatrol.dto.DevicePatrolRouteDeviceDto;
import com.quectel.core.module.devicepatrol.entity.DevicePatrolRouteDeviceEntity;
import com.quectel.core.module.devicepatrol.service.DevicePatrolRouteDeviceService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/06/10 10:33
 */
@DubboService
public class DevicePatrolRouteDeviceServiceImpl implements DevicePatrolRouteDeviceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DevicePatrolRouteDeviceServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID:%d", id);

    @Autowired
    private DevicePatrolRouteDeviceDao devicePatrolRouteDeviceDao;
    @Autowired
    private DeviceService deviceService;


    @Override
    public void deleteByRouteId(Long routeId) {
        devicePatrolRouteDeviceDao.delete(new LambdaQueryWrapper<DevicePatrolRouteDeviceEntity>().eq(DevicePatrolRouteDeviceEntity::getRouteId, routeId));
    }

    @Override
    public List<DevicePatrolRouteDeviceDto> queryListByRouteId(Long routeId) {
        List<DevicePatrolRouteDeviceEntity> entities = devicePatrolRouteDeviceDao.selectList(new LambdaQueryWrapper<DevicePatrolRouteDeviceEntity>().eq(DevicePatrolRouteDeviceEntity::getRouteId, routeId));
        List<DevicePatrolRouteDeviceDto> result = CopyUtils.copyList(entities, DevicePatrolRouteDeviceDto.class);
        List<DevicePatrolRouteDeviceDto> collect = result.stream().filter(patrolRouteDeviceDto -> {
            DeviceDto deviceDto = deviceService.selectCacheById(patrolRouteDeviceDto.getDeviceId());
            return deviceDto != null;
        }).sorted(Comparator.comparing(DevicePatrolRouteDeviceDto::getSeq)).collect(Collectors.toList());
        collect.forEach(this::paddingField);
        return collect;
    }

    @Override
    public DevicePatrolRouteDeviceDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DevicePatrolRouteDeviceDto selectById(Long id) {
        DevicePatrolRouteDeviceEntity entity = devicePatrolRouteDeviceDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, DevicePatrolRouteDeviceDto.class));
    }

    @Override
    public List<DevicePatrolRouteDeviceDto> queryList(Map<String, Object> params) {
        List<DevicePatrolRouteDeviceEntity> list = devicePatrolRouteDeviceDao.queryList(params);
        List<DevicePatrolRouteDeviceDto> result = CopyUtils.copyList(list, DevicePatrolRouteDeviceDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private DevicePatrolRouteDeviceDto paddingField(DevicePatrolRouteDeviceDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return devicePatrolRouteDeviceDao.queryTotal(params);
    }

    @Override
    public Long save(DevicePatrolRouteDeviceDto dto) {
        DevicePatrolRouteDeviceEntity entity = CopyUtils.copyObj(dto, DevicePatrolRouteDeviceEntity.class);
        entity.setId(Snowflake.nextId());
        devicePatrolRouteDeviceDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DevicePatrolRouteDeviceDto dto) {
        DevicePatrolRouteDeviceEntity entity = CopyUtils.copyObj(dto, DevicePatrolRouteDeviceEntity.class);
        devicePatrolRouteDeviceDao.updateById(entity);
        CacheKit.invalidRedisCache(DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(DevicePatrolRouteDeviceDto dto) {
        DevicePatrolRouteDeviceEntity entity = CopyUtils.copyObj(dto, DevicePatrolRouteDeviceEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, devicePatrolRouteDeviceDao);

        CacheKit.invalidRedisCache(DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        devicePatrolRouteDeviceDao.deleteById(id);

        CacheKit.invalidRedisCache(DEVICE_PATROL_ROUTE_DEVICE_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
